package scau.jump.securitytrace.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JOptionPane;

import scau.jump.securitytrace.init.Init;
import scau.jump.securitytrace.popupwindow.AddressWindow;
import scau.jump.securitytrace.util.AddressConvertUtil;

import com.securitytrace.model.Address;

public class UpdateAddressButtonListener implements ActionListener {

	private AddressWindow addressWindow;
	private Address address;
	
	public UpdateAddressButtonListener(AddressWindow addressWindow) {
		this.addressWindow = addressWindow;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void actionPerformed(ActionEvent e) {
		String name = addressWindow.getAddressNameTextField().getText();
		Vector<Vector<String>> vectors = addressWindow.getAddressModel().getDataVector();
		String desc = AddressConvertUtil.vectorToString(vectors);
		if(name.trim().isEmpty()) {
			JOptionPane.showMessageDialog(addressWindow, "登录信息名称不能为空");
		}else if(desc == null) {
			JOptionPane.showMessageDialog(addressWindow, "登录信息内容不能为空");
		}else {
			address.setAddressName(name);
			address.setOtherInfo(desc);
			try {
				updateAddressToDataBase(address);
				addressWindow.getManagerClient().setResultAddress(address);
				addressWindow.dispose();
			} catch(Exception ex) {
				JOptionPane.showMessageDialog(addressWindow, "操作失败");
			}
		}
	}
	
	private void updateAddressToDataBase(Address address) {
		Init.getAddressService().update(address);
		System.out.println("update address");
	}

	/**以下为get/set方法**/
	public Address getAddress() {
		return address;
	}
	
	public void setAddress(Address address) {
		this.address = address;
	}
}
